﻿using System;
using System.Linq;
using System.Windows.Forms;
using NTTDATA.BEENChina.ResultCertification.Main.ServiceResultCertification;
using NTTDATA.BEENChina.ResultCertification.model;

namespace NTTDATA.BEENChina.ResultCertification.UI
{
    public partial class FormCheckRunning : Form
    {
        private string _CusFormat = "yyyy-MM-dd HH:mm";
        private string _CusFormatB = "yyyy-MM-dd";



        public FormCheckRunning()
        {
            InitializeComponent();
            initDate();
        }

        private void initDate()
        {
            this.dateTimePickerFrom.Value =
                DateTime.Parse( DateTime.Now.AddDays(-3).ToString(_CusFormatB) + " 00:00:00");

            this.dateTimePickerTo.Value =
               DateTime.Parse( DateTime.Now.AddDays(-1).ToString(_CusFormatB) + " 00:00:00" );

            this.dateTimePickerRunTime.Value =
               DateTime.Parse(DateTime.Now.AddDays(0).ToString(_CusFormatB) + " 18:00:00");

        }

        private void Search()
        {
            //DataStore.IVerfity verfityAccess  = new VerfityAccess();
            //List<VerfityItem> verfityItems = verfityAccess.GetVerfityItems(GetSearcheCategoryType());

            
            using (ServiceResultCertificationClient client = new ServiceResultCertificationClient())
            {
                var checkSettings = client.GetCheckSettings();

               
               dataGridViewVerfityList.DataSource = checkSettings ;
                
            }
        }


        private void Delte()
        {
            var selectRows = this.dataGridViewVerfityList.SelectedRows;

            if (selectRows.Count > 0)
            {
                string id = selectRows[0].Cells["Id"].Value.ToString();
                using (ServiceResultCertificationClient client = new ServiceResultCertificationClient())
                {
                    client.DelCheckSetting(id);

                }
                 
            }
        }
         
        
 

        private void buttonSave_Click(object sender, EventArgs e)
        {
            try
            {
                string checkMsg = checkDataVaild();
                if (checkMsg != string.Empty)
                {
                    MessageBox.Show(checkMsg);
                    return;
                }

                var setting = new CheckSetting();

                setting.BeginDeliveryTime = this.dateTimePickerFrom.Value;
                setting.EndDeliveryTime = this.dateTimePickerTo.Value;
                setting.RunTime = this.dateTimePickerRunTime.Value;

                using (ServiceResultCertificationClient client = new ServiceResultCertificationClient())
                {
                    
                    client.AddCheckSetting(setting);
                }
                

                MessageBox.Show("保存成功");

                Search();
            }
            catch (Exception exp)
            {
                MessageBox.Show(exp.Message);
            }
        }

        private string checkDataVaild( )
        {            
            if (this.dateTimePickerFrom.CustomFormat == " ")
            {
                return  "请指定纳品开始时间";

            }
            if (this.dateTimePickerTo.CustomFormat == " ")
            {
                return "请指定纳品结束时间";
            }
            if (this.dateTimePickerRunTime.CustomFormat == " ")
            {
                return "请指定抽取时间";
            }
            return string.Empty ;
        }



        private void dateTimePickerFrom_ValueChanged(object sender, EventArgs e)
        {
            ((DateTimePicker)sender).Format = DateTimePickerFormat.Custom;
            ((DateTimePicker)sender).CustomFormat = _CusFormat;
        }

        private void buttonSearch_Click(object sender, EventArgs e)
        {
            Search();
        }

        private void buttonDel_Click(object sender, EventArgs e)
        {
            Delte();

            MessageBox.Show("保存成功");
            Search();

        }

        private void button1_Click(object sender, EventArgs e)
        {
            this.Close();
        }
    }
}